﻿(function(){
	com.yogi.core.PartWrapperBase = function(){		
		this.m_config;
		this.m_partConfig;
		this.m_part;
		this.m_ref_part;
	};
	com.yogi.core.PartWrapperBase.m_wrappers = {};
	Ext.apply( com.yogi.core.PartWrapperBase.prototype, {
		wrap: function(part,config,partConfig, concreteWrapper) { 	
			this.m_config = config;
			this.m_partConfig = partConfig;
			this.m_ref_part = this.m_partConfig.ref_part;
			this.m_part = part;	
			
			if( !Ext.isEmpty(config["guid"])){
				com.yogi.core.PartWrapperBase.m_wrappers[config["guid"]] = concreteWrapper;
			}
			if( !Ext.isEmpty(partConfig["guid"])){
				com.yogi.core.PartWrapperBase.m_wrappers[partConfig["guid"]] = concreteWrapper;
			}
		},
		getWrapper: function(guid) {
			return com.yogi.core.PartWrapperBase.m_wrappers[guid];
		},
		getFlexPart: function() {
			return this.m_ref_part ? this.m_ref_part : this.m_part;
		},
		getSelfPart: function() {
			return this.m_part;
		},		
		findAsset: function(asset_id, callback) {	
			var part = this.getFlexPart();
			var retry_times = 10;
			var wait_func = function() {
				var asset = part.getElement(asset_id);
				if(asset.length <= 0 && retry_times-- > 0){		//not ready yet, wait
					wait_func.defer(300);
				}else{
					callback(asset);
				}
			};
			wait_func();
		}
	});
	
	com.yogi.runtime.LoadingIndicateWrapper = function() {
		com.yogi.runtime.LoadingIndicateWrapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.runtime.LoadingIndicateWrapper,com.yogi.core.PartWrapperBase,{
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.runtime.LoadingIndicateWrapper.superclass.wrap(part,wrapperConfig,partConfig);			
		},
		progress: function(percent) {
			var part = com.yogi.runtime.LoadingIndicateWrapper.superclass.getFlexPart();			
			if(percent >= 100) {
				part.show(false);
			}else{
				part.show(true);
			}
		}
	});
	com.yogi.core.WrapperManager = function(){
		com.yogi.core.WrapperManager.WRAPPER_TYPE_REGISTERED = "com.yogi.core.WrapperManager.WRAPPER_TYPE_REGISTERED";
		//包裝器容器名與包裝器類別的查找表，key是包裝器類容器名，值是類定義數組
		var wrapper_ct_types_map = {};
		//包裝器類別與容器的查找表,key 是包裝器類型名，值是類容器名
		var wrapper_type_ct_map = {};
		var wrapper_map = {};		
		var _self;
		
		function doRegisterWrapperClass (wrapperType,cls)	{		//註冊包裝器類
			//alert("doRegisterWrapperClass:" + wrapperType + "-" + cls);
			wrapper_map[wrapperType] = cls;
		}		
		_self = {
				init: function(wrapper_json_array){
					if(!$.isArray(wrapper_json_array)){
						wrapper_json_array = $.parseJSON(wrapper_json_array);
					}
					doRegisterWrapperClass("loading", com.yogi.runtime.LoadingIndicateWrapper);
					$.each(wrapper_json_array,function(index, def){
						var ct  = def.ct;
						var classes = def.classes;
						if(classes)	{
							wrapper_ct_types_map[ct] = classes;				//正向查找表
							$.each(classes, function(c_idx,cls) {				//反向查找表
								wrapper_type_ct_map[cls.id] = ct;				//每個包裝器的類別==> 所屬的容器
							});
						}
					});
				},
				getWrapperInstance: function (wrapperType) {
					var m_wrapper = null;
					try
					{
						m_wrapper = new wrapper_map[wrapperType];	
					}
					catch(e)
					{
						throw ("lack of class:" + wrapperType + ".error:" + e);
					}
					return m_wrapper;	
				},			
				registerWrapperClass: function (wrapperType,cls)	{		//註冊包裝器類
					doRegisterWrapperClass(wrapperType,cls);
					$(_self).trigger(com.yogi.core.WrapperManager.WRAPPER_TYPE_REGISTERED, {
						type:wrapperType,
						cls:cls
					});
				},
				//判斷當前是否已註冊了包裝器類
				existsWrapperType: function(wrapperType){
					return wrapper_map.hasOwnProperty(wrapperType);
				},
				get_wrapper_ct_types_map: function(){
					return wrapper_ct_types_map;
				},
				get_wrapper_type_ct_map: function(){
					return wrapper_type_ct_map;
				},
				//根據包裝器類讀取其容器文件名(SWF)
				getWrapperCtName: function(wrapperType)	{					
					return wrapper_type_ct_map[wrapperType];
				}
		};
		return _self;
	};
	function dummy(){
		new com.yogi.runtime.LoadingIndicateWrapper();
		new com.yogi.core.WrapperManager();
	}
	$(window).trigger(com.yogi.core.EVENT_EXTERNAL_SCRIPT_LOADED,  { script:"com.yogi.core.WrapperManager" });
})();	